package com.bw.servlet;

import com.bw.dao.ClassesDao;
import com.bw.pojo.Classes;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/***************************
 FileAction:

 CreateTime:2022-04-13 9:06
 Author:Aaking-aline
 ****************************/
@WebServlet("/InsertServlet")
public class InsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @SneakyThrows
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Classes classes = new Classes();

        BeanUtils.populate(classes,parameterMap);


        try {
            new ClassesDao().insertClasses(classes);
            response.sendRedirect("success.jsp");
            return ;
        } catch (SQLException e) {
            response.sendRedirect("error.jsp");
            return ;
        }

    }
}
